package com.bao.shop.service.impl;

import com.bao.shop.entity.Goods;
import com.bao.shop.entity.GoodsName;
import com.bao.shop.entity.User;
import com.bao.shop.service.CartService;
import com.bao.shop.service.GoodsService;

import java.math.BigDecimal;
import java.sql.SQLOutput;
import java.util.ArrayList;

/**
 * @author baoye
 * @date 2024-07-25/19:07
 */
public class GoodsServiceImpl implements GoodsService {
    public CartService cartService = new CartServiceImpl();

    static {
        GOODS.add(new Goods("1001", GoodsName.四星冰箱, new BigDecimal("5555.55"), 40));
        GOODS.add(new Goods("1002", GoodsName.四星空调, new BigDecimal("9999.55"), 40));
        GOODS.add(new Goods("1003", GoodsName.地耳冰箱, new BigDecimal("6666.55"), 40));
        GOODS.add(new Goods("1004", GoodsName.地耳空调, new BigDecimal("8888.55"), 40));
        GOODS.add(new Goods("1005", GoodsName.海地冰箱, new BigDecimal("4999.99"), 40));
        GOODS.add(new Goods("1006", GoodsName.海耳冰箱, new BigDecimal("9999.99"), 40));
        GOODS.add(new Goods("1007", GoodsName.海陆冰箱, new BigDecimal("1515.15"), 40));
    }

    @Override
    public void updateStock(Goods goods, Integer num) {
        goods.setStock(goods.getStock() - num);
        if (goods.getStock() == 0) {
            removeGoods(goods);
        }
    }

    @Override
    public void removeGoods(Goods goods) {
        GOODS.remove(goods);
    }

    @Override
    public void buyGoods(User user) {
        if (GOODS.size() <= 0) {
            System.out.println("目前没有商品");
            return;
        }
        printGoods();
        Goods upGoods = null;

        int num = 0;
        boolean flag = true;
        while (flag) {
            System.out.print("请输入商品编号:");
            String id = INPUT.next();
            upGoods = findGoods(id);//直接=GOODS集合中的一个元素,修改这个等于修改集合
            if (upGoods == null) {
                System.out.println("商品编号不存在!");
                continue;
            }
            flag = false;
        }
        while (!flag) {
            System.out.print("请输入购买数量:");
            num = INPUT.nextInt();
            if (num <= 0) {
                System.out.println("购买数量不能为零或负数!");
                continue;
            }
            if (num > upGoods.getStock()) {
                System.out.println("购买数量不能超过" + upGoods.getStock());
                continue;
            }
            flag = true;
        }

        updateStock(upGoods, num);

        cartService.addGoodsToCart(user, upGoods, num);

        System.out.print("是否继续购买(Y/N):");
        if ("Y".equalsIgnoreCase(INPUT.next())) {
            cartService.pay(user);
            //跳转购物车
        } else {
            buyGoods(user);
            //回到购买页面
        }


    }

    @Override
    public Goods findGoods(String id) {
        for (Goods goods : GOODS) {
            if (goods.getGoodsId().equals(id)) {
                return goods;
            }
        }
        return null;
    }

    @Override
    public void printGoods() {
        for (Goods good : GOODS) {
            System.out.println(good);
        }
    }


}
